package org.edu.modules.sxjy.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PagingUtils
{
    public static Map<String, Object> getPageData(Object dataList, Integer pageNum, Integer pageSize)
    {
        List<Object> data = (List)dataList;
        Map<String, Object> resultMap = new HashMap();
        if (data.size() <= pageSize.intValue())
        {
            resultMap.put("data", data);
            resultMap.put("totalPage", Integer.valueOf(1));
            return resultMap;
        }
        List<List<Object>> allData = new ArrayList();
        int totalSize = data.size();
        int number = 0;
        while (totalSize >= pageSize.intValue())
        {
            int v1 = totalSize - pageSize.intValue();
            int v2 = totalSize - v1;

            List<Object> pageData = getData(data, Integer.valueOf(v2), Integer.valueOf(number));
            allData.add(pageData);
            number += v2;
            totalSize = v1;
        }
        if (totalSize > 0) {
            allData.add(getData(data, Integer.valueOf(data.size() - number), Integer.valueOf(number)));
        }
        List<Object> objects = (List)allData.get(pageNum.intValue() - 1);
        resultMap.put("data", objects);
        resultMap.put("totalPage", Integer.valueOf(allData.size()));
        return resultMap;
    }

    private static List<Object> getData(List<Object> data, Integer pageSize, Integer startIndex)
    {
        ArrayList<Object> list = new ArrayList();
        for (int i = startIndex.intValue(); i < pageSize.intValue() + startIndex.intValue(); i++) {
            list.add(data.get(i));
        }
        return list;
    }
}
